package com.lfy.product.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.lfy.product.bean.Product;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
@Slf4j
@RestController
@RequestMapping("/product")
public class ProductRestController {
    @SentinelResource(value = "getProduct", blockHandler = "getProductBlockHandler", fallback = "getProductFallback")
    @GetMapping("/get/{id}")
    public Product getProduct(@PathVariable("id") Long id, HttpServletRequest request){
        String header = request.getHeader("X-Service-Name");
        log.info("请求头信息{}",header);


        log.info("查询商品信息开始");
        Product product = new Product(id, new BigDecimal(100), "苹果-" + id, 100);
        log.info("查询{}商品信息，id:{}", id, product);
//模拟阻塞

//        try {
//            Thread.sleep(60000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
        return product;
    }
}
